package org.mybatis.generator.plugins;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.IntrospectedTable.TargetRuntime;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Interface;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;

public class TkMybatisPlugin extends PluginAdapter {

    public TkMybatisPlugin() {
        super();
    }

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        // import
        topLevelClass.addImportedType(new FullyQualifiedJavaType("javax.persistence.Column"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("javax.persistence.GeneratedValue"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("javax.persistence.GeneratedType"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("javax.persistence.Id"));
        topLevelClass.addImportedType(new FullyQualifiedJavaType("javax.persistence.Table"));

        // annotation
        String tableName = introspectedTable.getFullyQualifiedTable().getIntrospectedTableName();
        topLevelClass.addAnnotation("@Table(name = \""+tableName+"\")");

        return true;
    }

    @Override
    public boolean clientGenerated(Interface interfaze, IntrospectedTable introspectedTable) {

        if (introspectedTable.getTargetRuntime() == TargetRuntime.MYBATIS3) {
            // mapper接口文件添加extends Mapper<实体类>，并且加入import语句
            FullyQualifiedJavaType genericJavaType = new FullyQualifiedJavaType("Mapper<"
                    + introspectedTable.getFullyQualifiedTable().getDomainObjectName() + ">");
            FullyQualifiedJavaType imp = new FullyQualifiedJavaType("tk.mybatis.mapper.common.Mapper");
            FullyQualifiedJavaType domainObject = new FullyQualifiedJavaType(introspectedTable.getBaseRecordType());
            interfaze.addSuperInterface(genericJavaType);
            interfaze.addImportedType(domainObject);
            interfaze.addImportedType(imp);
        }
        return true;
    }

    @Override
    public boolean modelFieldGenerated(Field field, TopLevelClass topLevelClass,
                                       IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        // 在id字段上加入@Id和@GeneratedValue
        if (introspectedColumn.isAutoIncrement() || introspectedColumn.getActualColumnName().equals("id")) {
            field.addAnnotation("@Id");
            field.addAnnotation("@GeneratedValue(strategy = GenerationType.IDENTITY, generator = \"JDBC\")");
        }
        // 在每个字段声明上增加@Column注解
        field.addAnnotation("@Column(name = \"" + introspectedColumn.getActualColumnName() + "\")");
        return true;
    }
}
